/*!
// DatatypeExp v1.0
// Copyright 2015 OrChange Inc. All rights reserved.
// Licensed under the MIT License.
*/
/*jslint vars: true, sloppy: true, nomen: true, plusplus: true */
/*global Exp: false, expPattern: false, NumberExp:false,StringExp:false,BooleanExp:false */

// new ValueExp('/|#|!')
// new ValueExp('/^[a-e]*$|#1,2,6-10,<=-500|!')

var types = ['number', 'string', 'boolean', null];
var ValueExp = function ValueExp() {
	Exp.apply(this, arguments);

	this.$$allowNull = false;

	this.$$numberExp = null;
	this.$$booleanExp = null;
	this.$$stringExp = null;

	this.$initialization();
};
ValueExp.prototype = new Exp();
ValueExp.prototype.constructor = ValueExp;

ValueExp.prototype.$initialization = function () {
	var exps = {
		number: NumberExp,
		string: StringExp,
		boolean: BooleanExp
	};

	// TODO 如果正则里有|符号，需要被排除
	this.exp.split('|').forEach(function (exp) {

		types.forEach(function (typeName) {
			if (expPattern[typeName + 'ExpPattern'].test(exp)) {
				this['$$' + typeName + 'Exp'] = new exps[typeName](exp);
				return;
			}
		}, this);
	}, this);

};

ValueExp.prototype.$test = function (value) {
	if (value === null) {
		return this.$$allowNull;
	}

	var pattern = this['$$' + typeof value + 'Exp'];
	if (pattern !== null && pattern.test(value)) {
		return true;
	}

	return false;
};
